/**
 * 
 */
package com.toncen.samepms.common;

import java.util.Set;

import com.toncen.samepms.basic.domain.User;
import com.toncen.samepms.system.dao.MessageDao;
import com.toncen.samepms.system.domain.Advice;

/**
 * 描述：系统消息通知发布者
 * 
 * @author zq
 * 
 */
public class SystemMessageAdviceDispatcher extends AbstractAdviceDispatcher {
	/**
	 * 通知方式
	 * 
	 * 请查看系统编码配置：advice_mode
	 */
	private static final int MODE = 2;

	/**
	 * 
	 */
	public SystemMessageAdviceDispatcher() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAdviceDispatcher#accept(com.toncen.samepms.basic.domain.User,
	 *      com.toncen.samepms.system.domain.Advice)
	 */
	/**
	 * 当前用户对当前通知类型是否允许此中通知方式
	 * 
	 * @param user
	 * @param advice
	 * @return
	 */
	public boolean accept(User user, Advice advice) {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAdviceDispatcher#send(com.toncen.samepms.system.domain.Advice,
	 *      com.toncen.samepms.basic.domain.User, java.util.Set, java.lang.String,
	 *      java.lang.String)
	 */
	/**
	 * 发送通知
	 * 
	 * @param advice
	 *            通知
	 * @param fromUser
	 *            发送者
	 * @param toUsers
	 *            接收者
	 * @param subject
	 *            通知主题
	 * @param content
	 *            通知内容
	 * 
	 * @return
	 * @throws Exception
	 */
	public int send(Advice advice, User fromUser, Set<User> toUsers, String subject, String content) throws Exception {
		int result = VOID;
		StringBuffer sb = new StringBuffer();
		for (User toUser : toUsers) {
			sb.append(toUser.getId()).append(",");
		}
		result = (new MessageDao().saveAdvice(fromUser, sb.toString(), subject, content)) ? SUCCESSFUL : UNSUCCESSFUL;
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAdviceDispatcher#getMode()
	 */
	public int getMode() {
		return MODE;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAdviceDispatcher#send(java.lang.Object,
	 *      java.lang.String, java.lang.String)
	 */
	/**
	 * 发送通知
	 * 
	 * @param target
	 *            接收对象
	 * @param subject
	 *            通知主题
	 * @param content
	 *            通知内容
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean send(User target, String subject, String content) throws Exception {
		return new MessageDao().saveDefaultAdvice(target, subject, content);
	}
}
